<?php
error_reporting(E_ERROR | E_PARSE);
//test_house script script created by Robert Cox on August 1st 2011
//Commandline Usage: test_actiontracking.php commandline (debug)
//URL Usage: http://.../../test_actiontracking.php?web=1              <= normal mode
//      -or- http://.../../test_actiontracking.php?web=1&debug=1      <= debug mode

// set start time
$mtime = microtime();
$mtime = explode(' ', $mtime);
$mtime = $mtime[1] + $mtime[0];
$starttime = $mtime;

if(isset($_GET['debug'])){
                $debug = TRUE;
                $GLOBALS["debug"] = TRUE;
        } else {
                unset($debug);
        }

if(isset($_GET['web'])){
                $web = TRUE;
                $GLOBALS["web"] = TRUE;
        } else {
                unset($web);
        }


if (isset($debug)) { 
	echo "<br>* * * * *  D E B U G = O N * * * * *<br>";
	if (isset($web)) { 
		echo "<br>* * * * *  W E B = O N  * * * * *<br>";}
}

//if (isset($web)) { 	echo "<br>This test will take approximately 4 minutes.<br>";}


$status = "pending";
$testname = "Action Tracking Test";
$testshortname = "actiontrackingtest";
$testdesc = "Verify that Standard and Custom Action Tracking Are Working";
$testurl = "test_actiontracking.php";
$testtime = time();


$today = date("F j, Y, g:i a");
$hour = date("G");
$datestamp = date("Y-m-d");
$random = rand();
$Targeting="\"house\"";


// setup command line args (commandline only)
if (isset($argv[1])) { $commandline = $argv[1]; unset($debug);} 

// Username, password and schema for the database connection.
$username="aimatch";
$password="RMSAdmin";
$database="canary";

// Connect to the database and run the query
mysql_connect('w19',$username,$password);
@mysql_select_db($database) or die( "Unable to select database");

// Retrieve Current Numbers from the database
$query = "select s2.flight_creative_id, s2.site, s2.area, s2.served, s2.clicked, s2.acted, s1.action_id, s1.attribution, s1.acted ";
$query = $query." from ( ";
$query = $query."select date, flight_creative_id, site, area, served, clicked, acted ";
$query = $query."from bigsum where date=\"".$datestamp."\"  ";
$query = $query." and flight_creative_id in (34,38,31,38,35,36,37,30,32) ";
$query = $query." order by flight_creative_id ";
$query = $query.") as s2 ";
$query = $query."left outer join (  ";
$query = $query."select date, flight_creative_id, action_id, attribution, site, area, acted ";
$query = $query."from actionsum where date=\"".$datestamp."\" ";
$query = $query."and flight_creative_id in (35,36,37) ";
$query = $query."order by flight_creative_id ";
$query = $query.") as s1 ";
$query = $query."on s1.flight_creative_id = s2.flight_creative_id ";
$query = $query."group by s2.flight_creative_id ";
$query = $query."order by s2.flight_creative_id ";

if($debug){ echo "BigSum Query: <br>".$query."<br>"; }
// Retrieve the query results
$before_result=mysql_query($query);

// Determine the number of rows in the result set.
if($before_result){
    $before_num=mysql_numrows($before_result); }
else {
    $before_num=0; }

$i=0;

$commandline_output = $commandline_output."\n--------------------------------------------------------------------------------------------------------------------------\n"; 
$web_output = "<br><font face=\"Tahoma\" size=\"3\"><b>Table Results Before Test:</b></font><br>"; 
$commandline_output = "\nTable Results Before Test:\n"; 

if($before_num){
	while($i < $before_num){
		$before_FlightCreativeID[$i]=mysql_result($before_result,$i,"s2.flight_creative_id");
		$before_Site[$i]=mysql_result($before_result,$i,"s2.site");
		$before_Area[$i]=mysql_result($before_result,$i,"s2.area");
		$before_Served[$i]=mysql_result($before_result,$i,"s2.served");
		$before_Clicked[$i]=mysql_result($before_result,$i,"s2.clicked");
		$before_BSActed[$i]=mysql_result($before_result,$i,"s2.acted");
		$before_ASActed[$i]=mysql_result($before_result,$i,"s1.acted");
		$before_ActionID[$i]=mysql_result($before_result,$i,"s1.action_id");
		$before_Attribution[$i]=mysql_result($before_result,$i,"s1.attribution");
		$web_output = $web_output."<font face=\"Tahoma\" size=\"3\">FlightID: ".$before_FlightCreativeID[$i]." | "; 
		$commandline_output = $commandline_output."     FlightID: ".$before_FlightCreativeID[$i]." | "; 
		$web_output = $web_output."Site: ".$before_Site[$i]." | "; 
		$commandline_output = $commandline_output." Site: ".$before_Site[$i]." | "; 
		$web_output = $web_output."Area: ".$before_Area[$i]." | "; 
		$commandline_output = $commandline_output." Area: ".$before_Area[$i]." | "; 
		$web_output = $web_output."Served: ".$before_Served[$i]." | "; 
		$commandline_output = $commandline_output." Served: ".$before_Served[$i]." | "; 
		$web_output = $web_output."Clicked: ".$before_Clicked[$i]." | "; 
		$commandline_output = $commandline_output." Clicked: ".$before_Clicked[$i]." | "; 
		$web_output = $web_output."BSActed: ".$before_BSActed[$i]." | ";
		$commandline_output = $commandline_output." BSActed: ".$before_BSActed[$i]." | ";
		$web_output = $web_output."ASActed: ".$before_ASActed[$i]." | "; 
		$commandline_output = $commandline_output." ASActed: ".$before_ASActed[$i]." | "; 
		$web_output = $web_output."ActionID: ".$before_ActionID[$i]." | "; 
		$commandline_output = $commandline_output." ActionID: ".$before_ActionID[$i]." | "; 
		$web_output = $web_output."Attribution: ".$before_Attribution[$i]."</font><br>"; 
		$commandline_output = $commandline_output." Attribution: ".$before_Attribution[$i]."\n"; 
		$i++;
	}
}
else {

		$before_FlightCreativeID = array(30, 31, 32, 34, 35, 36, 37, 38);
		            $before_Site = array(2, 2, 2, 1, 1, 1, 1, 1);
		            $before_Area = array(8, 6, 6, 6, 8, 8, 8, 6);
		          $before_Served = array(0, 0, 0, 0, 0, 0, 0, 0);
		         $before_Clicked = array(0, 0, 0, 0, 0, 0, 0, 0);
		         $before_BSActed = array(0, 0, 0, 0, 0, 0, 0, 0);
		        $before_ActionID = array(0, 0, 0, 0, 7, 8, 9, 0);
		     $before_Attribution = array(0, 0, 0, 0, 1, 0, 1, 0);
		         $before_ASActed = array(0, 0, 0, 0, 0, 0, 0, 0);
		$i=0;
		$before_num = 8;
		while($i < 8){
			$web_output = $web_output."<font face=\"Tahoma\" size=\"3\">FlightID: ".$before_FlightCreativeID[$i]." | "; 
			$commandline_output = $commandline_output."     FlightID: ".$before_FlightCreativeID[$i]." | "; 
			$web_output = $web_output."Site: ".$before_Site[$i]." | "; 
			$commandline_output = $commandline_output."  Site: ".$before_Site[$i]." | "; 
			$web_output = $web_output."Area: ".$before_Area[$i]." | "; 
			$commandline_output = $commandline_output."  Area: ".$before_Area[$i]." | "; 
			$web_output = $web_output."Served: ".$before_Served[$i]." | "; 
			$commandline_output = $commandline_output."  Served: ".$before_Served[$i]." | "; 
			$web_output = $web_output."Clicked: ".$before_Clicked[$i]." | "; 
			$commandline_output = $commandline_output."  Clicked: ".$before_Clicked[$i]." | "; 
			$web_output = $web_output."BSActed: ".$before_BSActed[$i]." | "; 
			$commandline_output = $commandline_output."  BSActed: ".$before_BSActed[$i]." | "; 
			$web_output = $web_output."ASActed: ".$before_ASActed[$i]." | "; 
			$commandline_output = $commandline_output."  ASActed: ".$before_ASActed[$i]." | "; 
			$web_output = $web_output."ActionID: ".$before_ActionID[$i]." | "; 
			$commandline_output = $commandline_output."  ActionID: ".$before_ActionID[$i]." | "; 
			$web_output = $web_output."Attribution: ".$before_Attribution[$i]."</font><br>"; 
			$commandline_output = $commandline_output."  Attribution: ".$before_Attribution[$i]."\n"; 

			$i++; }
}
	$commandline_output = $commandline_output."\n--------------------------------------------------------------------------------------------------------------------------\n"; 
//
// Standard Action Test #1: Test Action Logging to advertisers with and without the ACT=3 directive
// 1.1 Request an impression from the first flight for the Canary Advertiser
// http://crtldev.aimatch.com:60000/canary/hserver/mid={random_number}/site=canary/area=standard_action_tracking/standard_action=1
//
	$web_output = $web_output."<br><font face=\"Tahoma\" size=\"5\"><b>Test Batch 1: Standard Action Test</b></font><br>"; 
	$commandline_output = $commandline_output."Test Batch 1: Standard Action Test\n"; 
	$web_output = $web_output."<font face=\"Tahoma\" size=\"4\"><b>Test Batch Description: Test Action Logging to Advertisers with and without the ACT=3 directive</b></font><br>"; 
	$commandline_output = $commandline_output."Test Batch Description: Test Action Logging to Advertisers with and without the ACT=3 directive\n"; 
	$web_output = $web_output."<hr>"; 
	$commandline_output = $commandline_output."\n--------------------------------------------------------------------------------------------------------------------------\n"; 
	$web_output = $web_output."<font face=\"Tahoma\" size=\"3\" COLOR=\"#FF0000\"><b>Test 1.1: Request an impression from the first flight for the Canary Advertiser</b><br></font><br>"; 
	$commandline_output = $commandline_output."Test 1.1: Request an impression from the first flight for the Canary Advertiser\n\n"; 

//    $url = "http://crtldev.aimatch.com:60000/canary/hserver/mid=".$testtime."1a/site=canary/area=standard_action_tracking/standard_action=1";
    $url = "http://crtldev.aimatch.com:60000/canary/hserver/mid=".$testtime."1a/site=canary/area=standard_action_tracking/standard_action=1/trace=3";
	$output = MakeAdCall($url);
	$output_array = MakeOutputArray($output);
	$formatted_output = GetFormattedOutput($output_array,$web,$debug);
	$text1 = "The correct ad was returned for the ad call that was made";
	$text2 = "";
	$text3 = "The response to the ad call returned:";
	list($status11,$web_message1,$web_message2,$web_message3,$cl_message1,$cl_message2,$cl_message3) = CheckResponse($text1,$text2,$text3,$output,"StandardActionTracking-1.jpg",$web);
	$web_output = $web_output.$web_message1.$web_message2.$web_message3."<hr>"; 
	$commandline_output = $commandline_output.$cl_message1.$cl_message2.$cl_message3."\n"; 
	
//
// 1.2 Send an action count request to the advertiser using the /ACT=3 directive
// http://crtldev.aimatch.com:60000/canary/count/site=canary/area=standard_action_tracking/standard_action=1/advid=1/act=3
//
	$commandline_output = $commandline_output."          -----------------\n"; 
	$web_output = $web_output."<font face=\"Tahoma\" size=\"3\" COLOR=\"#FF0000\"><b>Test 1.2: Send an action count request ot the advertiser using the ACT=3 directive</b></font><br><br>"; 
	$commandline_output = $commandline_output."Test 1.2: Send an action count request ot the advertiser using the ACT=3 directive\n\n"; 

//    $url = "http://crtldev.aimatch.com:60000/canary/count/mid=".$testtime."1a/site=canary/area=standard_action_tracking/standard_action=1/advid=1/act=3";
    $url = "http://crtldev.aimatch.com:60000/canary/count/mid=".$testtime."1a/site=canary/area=standard_action_tracking/standard_action=1/advid=1/act=3/trace=3";
	$output = MakeAdCall($url);
	$output_array = MakeOutputArray($output);
	$formatted_output = GetFormattedOutput($output_array,$web,$debug);
	$text1 = "A valid response was recieved from the Action Request ";
	$text2 = "";
	$text3 = "The response to the action request was:";
	list($status12,$web_message1,$web_message2,$web_message3,$cl_message1,$cl_message2,$cl_message3) = CheckResponse($text1,$text2,$text3,$output,"GIF89a",$web);
	$web_output = $web_output.$web_message1.$web_message2.$web_message3."<hr>"; 
	$commandline_output = $commandline_output.$cl_message1.$cl_message2.$cl_message3."\n"; 

//
// 1.3 Send a click from the second flight for the Canary Advertiser
// http://crtldev.aimatch.com:60000/canary/adclick/FCID=38/site=canary/area=standard_action_tracking/standard_action=2
//
	$commandline_output = $commandline_output."          -----------------\n"; 
	$web_output = $web_output."<font face=\"Tahoma\" size=\"3\" COLOR=\"#FF0000\"><b>Test 1.3: Send a click request for the second flight for the Canary advertiser</b></font><br><br>"; 
	$commandline_output = $commandline_output."Test 1.3: Send a click request for the second flight for the Canary advertiser\n\n"; 
//    $url = "http://crtldev.aimatch.com:60000/canary/adclick/mid=".$testtime."1b/FCID=38/site=canary/area=standard_action_tracking/standard_action=2/advid=1";
    $url = "http://crtldev.aimatch.com:60000/canary/adclick/mid=".$testtime."1b/FCID=38/site=canary/area=standard_action_tracking/standard_action=2/advid=1/trace=3";
	$output = MakeAdCall($url);
	$output_array = MakeOutputArray($output);
	$formatted_output = GetFormattedOutput($output_array,$web,$debug);
	$text1 = "A valid response was recieved to the click request ";
	$text2 = "";
	$text3 = "The 302 Redirect Location was: ";
	list($status13,$web_message1,$web_message2,$web_message3,$cl_message1,$cl_message2,$cl_message3) = CheckClickResponse($text1,$text2,$text3,$output_array,"tests.aimatch.com",$web,$debug);
	$web_output = $web_output.$web_message1.$web_message2.$web_message3."<hr>"; 
	$commandline_output = $commandline_output.$cl_message1.$cl_message2.$cl_message3."\n"; 

//
// 1.4 Send an action count request to the advertiser using just the ADVID= directive
// http://crtldev.aimatch.com:60000/canary/count/site=canary/area=standard_action_tracking/standard_action=2/advid=1
//
	$commandline_output = $commandline_output."          -----------------\n"; 
	$web_output = $web_output."<font face=\"Tahoma\" size=\"3\" COLOR=\"#FF0000\"><b>Test 1.4: Send an action count request to the advertiser using just the ADVID directive</b></font><br><br>"; 
	$commandline_output = $commandline_output."Test 1.4: Send an action count request to the advertiser using just the ADVID directive\n\n"; 
//    $url = "http://crtldev.aimatch.com:60000/canary/count/mid=".$testtime."1b/site=canary/area=standard_action_tracking/standard_action=2/advid=1";
    $url = "http://crtldev.aimatch.com:60000/canary/count/mid=".$testtime."1b/site=canary/area=standard_action_tracking/standard_action=2/advid=1/trace=3";
	$output = MakeAdCall($url);
	$output_array = MakeOutputArray($output);
	$formatted_output = GetFormattedOutput($output_array,$web,$debug);
	$text1 = "A valid response was recieved from the Action Request ";
	$text2 = "";
	$text3 = "The response to the action request was:";
	list($status14,$web_message1,$web_message2,$web_message3,$cl_message1,$cl_message2,$cl_message3) = CheckResponse($text1,$text2,$text3,$output,"GIF89a",$web);
	$web_output = $web_output.$web_message1.$web_message2.$web_message3."<hr>"; 
	$commandline_output = $commandline_output.$cl_message1.$cl_message2.$cl_message3."\n"; 

//
// Standard Action Test #2: Test that there are no actions logged to advertisers with a CPA
// 2.1 Request an impression from the first flight for the Sylvester Advertiser
// http://crtldev.aimatch.com:60000/canary/hserver/mid={random_number}/site=sylvester/area=standard_action_tracking/standard_action=3
//
	$commandline_output = $commandline_output."\n--------------------------------------------------------------------------------------------------------------------------\n"; 
	$web_output = $web_output."<br><font face=\"Tahoma\" size=\"5\"><b>Test Batch 2: Standard Action Test</b></font><br>"; 
	$commandline_output = $commandline_output."Test Batch 2: Standard Action Test\n"; 
	$web_output = $web_output."<font face=\"Tahoma\" size=\"4\"><b>Test Batch Description: Test that no actions are logged to advertisers with a CPA</b></font><br>"; 
	$commandline_output = $commandline_output."Test Batch Description: Test that no actions are logged to advertisers with a CPA\n"; 
	$web_output = $web_output."<hr>"; 
	$commandline_output = $commandline_output."\n--------------------------------------------------------------------------------------------------------------------------\n"; 
	$web_output = $web_output."<font face=\"Tahoma\" size=\"3\" COLOR=\"#FF0000\"><b>Test 2.1: Request an impression from the first flight for the Sylvester Advertiser</b><br></font><br>"; 
	$commandline_output = $commandline_output."Test 2.1: Request an impression from the first flight for the Sylvester Advertiser\n\n"; 

    $url = "http://crtldev.aimatch.com:60000/canary/hserver/mid=".$testtime."2A/site=sylvester/area=standard_action_tracking/standard_action=3/trace=3";
	$output = MakeAdCall($url);
	$output_array = MakeOutputArray($output);
	$formatted_output = GetFormattedOutput($output_array,$web,$debug);
	$text1 = "The correct ad was returned for the ad call that was made";
	$text2 = "";
	$text3 = "The response to the ad call returned:";
	list($status21,$web_message1,$web_message2,$web_message3,$cl_message1,$cl_message2,$cl_message3) = CheckResponse($text1,$text2,$text3,$output,"StandardActionTracking-3.jpg",$web);
	$web_output = $web_output.$web_message1.$web_message2.$web_message3."<hr>"; 
	$commandline_output = $commandline_output.$cl_message1.$cl_message2.$cl_message3."\n"; 

//
// 2.2 Send an action count request to the advertiser using the /ACT=3 directive
// http://crtldev.aimatch.com:60000/canary/count/site=sylvester/area=standard_action_tracking/standard_action=3/advid=2/act=3
//
	$commandline_output = $commandline_output."          -----------------\n"; 
	$web_output = $web_output."<font face=\"Tahoma\" size=\"3\" COLOR=\"#FF0000\"><b>Test 2.2: Send an action count request ot the advertiser using the ACT=3 directive</b></font><br><br>"; 
	$commandline_output = $commandline_output."Test 2.2: Send an action count request ot the advertiser using the ACT=3 directive\n"; 
    $url = "http://crtldev.aimatch.com:60000/canary/count/mid=".$testtime."2A/site=sylvester/area=standard_action_tracking/standard_action=3/advid=2/act=3/trace=3";
	$output = MakeAdCall($url);
	$output_array = MakeOutputArray($output);
	$formatted_output = GetFormattedOutput($output_array,$web,$debug);
	$text1 = "A valid response was recieved from the Action Request ";
	$text2 = "";
	$text3 = "The response to the action request was:";
	list($status22,$web_message1,$web_message2,$web_message3,$cl_message1,$cl_message2,$cl_message3) = CheckResponse($text1,$text2,$text3,$output,"GIF89a",$web);
	$web_output = $web_output.$web_message1.$web_message2.$web_message3."<hr>"; 
	$commandline_output = $commandline_output.$cl_message1.$cl_message2.$cl_message3."\n"; 
	
//
// 2.3 Send a click from the second flight for the Canary Advertiser
// http://crtldev.aimatch.com:60000/canary/adclick/FCID=38/site=sylvester/area=standard_action_tracking/standard_action=3
//
	$commandline_output = $commandline_output."          -----------------\n"; 
	$web_output = $web_output."<font face=\"Tahoma\" size=\"3\" COLOR=\"#FF0000\"><b>Test 2.3: Send a click request for the second flight for the Sylvester advertiser</b></font><br><br>"; 
	$commandline_output = $commandline_output."Test 2.3: Send a click request for the second flight for the Sylvester advertiser\n"; 
    $url = "http://crtldev.aimatch.com:60000/canary/adclick/mid=".$testtime."2B/FCID=32/site=sylvester/area=standard_action_tracking/standard_action=4/advid=2/trace=3";
	$output = MakeAdCall($url);
	$output_array = MakeOutputArray($output);
	$formatted_output = GetFormattedOutput($output_array,$web,$debug);
	$text1 = "A valid response was recieved to the click request ";
	$text2 = "";
	$text3 = "The 302 Redirect Location was: ";
	list($status23,$web_message1,$web_message2,$web_message3,$cl_message1,$cl_message2,$cl_message3) = CheckClickResponse($text1,$text2,$text3,$output_array,"tests.aimatch.com",$web,$debug);
	$web_output = $web_output.$web_message1.$web_message2.$web_message3."<hr>"; 
	$commandline_output = $commandline_output.$cl_message1.$cl_message2.$cl_message3."\n"; 

//
// 2.4 Send an action count request to the advertiser using just the ADVID= directive
// http://crtldev.aimatch.com:60000/canary/count/site=sylvester/area=standard_action_tracking/standard_action=4/advid=2
//
	$commandline_output = $commandline_output."          -----------------\n"; 
	$web_output = $web_output."<font face=\"Tahoma\" size=\"3\" COLOR=\"#FF0000\"><b>Test 2.4: Send an action count request to the advertiser using just the ADVID directive</b></font><br><br>"; 
	$commandline_output = $commandline_output."Test 2.4: Send an action count request to the advertiser using just the ADVID directive\n"; 
    $url = "http://crtldev.aimatch.com:60000/canary/count/mid=".$testtime."2B/site=sylvester/area=standard_action_tracking/standard_action=4/advid=2/trace=3";
	$output = MakeAdCall($url);
	$output_array = MakeOutputArray($output);
	$formatted_output = GetFormattedOutput($output_array,$web,$debug);
	$text1 = "A valid response was recieved from the Action Request ";
	$text2 = "";
	$text3 = "The response to the action request was:";
	list($status24,$web_message1,$web_message2,$web_message3,$cl_message1,$cl_message2,$cl_message3) = CheckResponse($text1,$text2,$text3,$output,"GIF89a",$web);
	$web_output = $web_output.$web_message1.$web_message2.$web_message3."<hr>"; 
	$commandline_output = $commandline_output.$cl_message1.$cl_message2.$cl_message3."\n"; 

//
// Custom Action Test #1: Test action logging to an advertiser using the ACTID directive
// 3.1 Request an impression from the first custom action flight for the Canary advertiser
// http://crtldev.aimatch.com:60000/canary/hserver/mid={random_number}/site=canary/area=custom_action_tracking/custom_action=1
//
	$commandline_output = $commandline_output."\n--------------------------------------------------------------------------------------------------------------------------\n"; 
	$web_output = $web_output."<br><font face=\"Tahoma\" size=\"5\"><b>Test Batch 3: Custom Action Test</b></font><br>"; 
	$commandline_output = $commandline_output."Test Batch 3: Custom Action Test\n"; 
	$web_output = $web_output."<font face=\"Tahoma\" size=\"4\"><b>Test Batch Description: Test action logging to an advertiser using the ACTID directive</b></font><br>"; 
	$commandline_output = $commandline_output."Test Batch Description: Test action logging to an advertiser using the ACTID directive\n"; 
	$web_output = $web_output."<hr>"; 
	$commandline_output = $commandline_output."\n--------------------------------------------------------------------------------------------------------------------------\n"; 
	$web_output = $web_output."<font face=\"Tahoma\" size=\"3\" COLOR=\"#FF0000\"><b>Test 3.1: Request an impression from the first custom action flight for the Canary Advertiser</b><br></font><br>"; 
	$commandline_output = $commandline_output."Test 3.1: Request an impression from the first custom action flight for the Canary Advertiser\n\n"; 
    $url = "http://crtldev.aimatch.com:60000/canary/hserver/mid=".$testtime."3A/site=canary/area=custom_action_tracking/custom_action=1/trace=3";
	$output = MakeAdCall($url);
	$output_array = MakeOutputArray($output);
	$formatted_output = GetFormattedOutput($output_array,$web,$debug);
	$text1 = "The correct ad was returned for the ad call that was made";
	$text2 = "";
	$text3 = "The response to the ad call returned:";
	list($status31,$web_message1,$web_message2,$web_message3,$cl_message1,$cl_message2,$cl_message3) = CheckResponse($text1,$text2,$text3,$output,"CustomActionTracking-1.jpg",$web);
	$web_output = $web_output.$web_message1.$web_message2.$web_message3."<hr>"; 
	$commandline_output = $commandline_output.$cl_message1.$cl_message2.$cl_message3."\n"; 

//
// 3.2 Send a click to the first custom action flight for the Canary Advertiser
// http://crtldev.aimatch.com:60000/canary/adclick/FCID=35/site=canary/area=custom_action_tracking/custom_action=1
//
	sleep(2); // sleep for two seconds before submitting the click
	$commandline_output = $commandline_output."          -----------------\n"; 
	$web_output = $web_output."<font face=\"Tahoma\" size=\"3\" COLOR=\"#FF0000\"><b>Test 3.2: Send a click to the first custom action flight for the Canary advertiser</b></font><br><br>"; 
	$commandline_output = $commandline_output."Test 3.2: Send a click to the first custom action flight for the Canary advertiser\n\n"; 
    $url = "http://crtldev.aimatch.com:60000/canary/adclick/mid=".$testtime."3A/FCID=35/site=canary/area=custom_action_tracking/custom_action=1/trace=3";
	$output = MakeAdCall($url);
	$output_array = MakeOutputArray($output);
	$formatted_output = GetFormattedOutput($output_array,$web,$debug);
	$text1 = "A valid response was recieved to the click request ";
	$text2 = "";
	$text3 = "The 302 Redirect Location was: ";
	list($status32,$web_message1,$web_message2,$web_message3,$cl_message1,$cl_message2,$cl_message3) = CheckClickResponse($text1,$text2,$text3,$output_array,"tests.aimatch.com",$web,$debug);
	$web_output = $web_output.$web_message1.$web_message2.$web_message3."<hr>"; 
	$commandline_output = $commandline_output.$cl_message1.$cl_message2.$cl_message3."\n"; 

//
// 3.3 Send an action count request to the advertiser using just the ACTID= directive
// http://crtldev.aimatch.com:60000/canary/count/advid=1/actid=7
//
	$commandline_output = $commandline_output."          -----------------\n"; 
	$web_output = $web_output."<font face=\"Tahoma\" size=\"3\" COLOR=\"#FF0000\"><b>Test 3.3: Send an action count request to the advertiser using just the ACTID= directive</b></font><br><br>"; 
	$commandline_output = $commandline_output."Test 3.3: Send an action count request to the advertiser using just the ACTID= directive\n\n"; 
    $url = "http://crtldev.aimatch.com:60000/canary/count/mid=".$testtime."3A/advid=1/ACTID=7/trace=3";
	$output = MakeAdCall($url);
	$output_array = MakeOutputArray($output);
	$formatted_output = GetFormattedOutput($output_array,$web,$debug);
	$text1 = "A valid response was recieved from the Action Request ";
	$text2 = "";
	$text3 = "The response to the action request was:";
	list($status33,$web_message1,$web_message2,$web_message3,$cl_message1,$cl_message2,$cl_message3) = CheckResponse($text1,$text2,$text3,$output,"GIF89a",$web);
	$web_output = $web_output.$web_message1.$web_message2.$web_message3."<hr>"; 
	$commandline_output = $commandline_output.$cl_message1.$cl_message2.$cl_message3."\n"; 

//
// Custom Action Test #4: Test action logging to an advertiser using the ACTNAME directive
// 4.1 Send a click to the second custom action flight for the Canary Advertiser
// http://crtldev.aimatch.com:60000/canary/adclick/FCID=36/site=canary/area=custom_action_tracking/standard_action=2
//
	$commandline_output = $commandline_output."\n--------------------------------------------------------------------------------------------------------------------------\n"; 
	$web_output = $web_output."<br><font face=\"Tahoma\" size=\"5\"><b>Test Batch 4: Custom Action Test</b></font><br>"; 
	$commandline_output = $commandline_output."Test Batch 4: Custom Action Test\n"; 
	$web_output = $web_output."<font face=\"Tahoma\" size=\"4\"><b>Test Batch Description: Test action logging to an advertiser using the ACTNAME directive</b></font><br>"; 
	$commandline_output = $commandline_output."Test Batch Description: Test action logging to an advertiser using the ACTNAME directive\n"; 
	$web_output = $web_output."<hr>"; 
	$commandline_output = $commandline_output."\n--------------------------------------------------------------------------------------------------------------------------\n"; 
	$web_output = $web_output."<font face=\"Tahoma\" size=\"3\" COLOR=\"#FF0000\"><b>Test 4.1: Send a click to the second custom action flight for the Canary Advertiser</b></font><br><br>"; 
	$commandline_output = $commandline_output."Test 4.1: Send a click to the second custom action flight for the Canary Advertiser\n\n"; 
    $url = "http://crtldev.aimatch.com:60000/canary/adclick/mid=".$testtime."4A/FCID=36/site=canary/area=custom_action_tracking/custom_action=2/trace=3";
	$output = MakeAdCall($url);
	$output_array = MakeOutputArray($output);
	$formatted_output = GetFormattedOutput($output_array,$web,$debug);
	$text1 = "A valid response was recieved to the click request ";
	$text2 = "";
	$text3 = "The 302 Redirect Location was: ";
	list($status41,$web_message1,$web_message2,$web_message3,$cl_message1,$cl_message2,$cl_message3) = CheckClickResponse($text1,$text2,$text3,$output_array,"tests.aimatch.com",$web,$debug);
	$web_output = $web_output.$web_message1.$web_message2.$web_message3."<hr>"; 
	$commandline_output = $commandline_output.$cl_message1.$cl_message2.$cl_message3."\n"; 

//
// 4.2 Request an impression from the second custom action flight for the Canary advertiser
// http://crtldev.aimatch.com:60000/canary/hserver/site=canary/area=custom_action_tracking/custom_action=2
//
	sleep(2); // sleep for two seconds before submitting the click
	$commandline_output = $commandline_output."          -----------------\n"; 
	$web_output = $web_output."<font face=\"Tahoma\" size=\"3\" COLOR=\"#FF0000\"><b>Test 4.2: Request an impression from the second custom action flight for the Canary advertiser</b><br></font><br>"; 
	$commandline_output = $commandline_output."Test 4.2: Request an impression from the second custom action flight for the Canary advertiser\n\n"; 
    $url = "http://crtldev.aimatch.com:60000/canary/hserver/mid=".$testtime."4A/site=canary/area=custom_action_tracking/custom_action=2/trace=3";
	$output = MakeAdCall($url);
	$output_array = MakeOutputArray($output);
	$formatted_output = GetFormattedOutput($output_array,$web,$debug);
	$text1 = "The correct ad was returned for the ad call that was made";
	$text2 = "";
	$text3 = "The response to the ad call returned:";
	list($status42,$web_message1,$web_message2,$web_message3,$cl_message1,$cl_message2,$cl_message3) = CheckResponse($text1,$text2,$text3,$output,"CustomActionTracking-2.jpg",$web);
	$web_output = $web_output.$web_message1.$web_message2.$web_message3."<hr>"; 
	$commandline_output = $commandline_output.$cl_message1.$cl_message2.$cl_message3."\n"; 
	
//
// 4.3 Send an action count request to the advertiser using just the ACTNAME= directive
// http://crtldev.aimatch.com:60000/canary/count/advid=1/actname=canarypoorpuddytat
//
	$commandline_output = $commandline_output."          -----------------\n"; 
	$web_output = $web_output."<font face=\"Tahoma\" size=\"3\" COLOR=\"#FF0000\"><b>Test 4.3: Send an action count request to the advertiser using just the ACTNAME= directive</b></font><br><br>"; 
	$commandline_output = $commandline_output."Test 4.3: Send an action count request to the advertiser using just the ACTNAME= directive\n\n"; 
    $url = "http://crtldev.aimatch.com:60000/canary/count/mid=".$testtime."4A/advid=1/ACTNAME=canarypoorpuddytat/trace=3";
	$output = MakeAdCall($url);
	$output_array = MakeOutputArray($output);
	$formatted_output = GetFormattedOutput($output_array,$web,$debug);
	$text1 = "A valid response was recieved from the Action Request ";
	$text2 = "";
	$text3 = "The response to the action request was:";
	list($status43,$web_message1,$web_message2,$web_message3,$cl_message1,$cl_message2,$cl_message3) = CheckResponse($text1,$text2,$text3,$output,"GIF89a",$web);
	$web_output = $web_output.$web_message1.$web_message2.$web_message3."<hr>"; 
	$commandline_output = $commandline_output.$cl_message1.$cl_message2.$cl_message3."\n"; 

//
// Custom Action Test #5: Test action logging to an advertiser with the "Click Trumps Impression" Flag set
// 5.1 Send a click to the third custom action flight for the Canary Advertiser
// http://crtldev.aimatch.com:60000/canary/adclick/FCID=37/site=canary/area=custom_action_tracking/standard_action=3
//
	$commandline_output = $commandline_output."\n--------------------------------------------------------------------------------------------------------------------------\n"; 
	$web_output = $web_output."<br><font face=\"Tahoma\" size=\"5\"><b>Test Batch 5: Custom Action Test</b></font><br>"; 
	$commandline_output = $commandline_output."Test Batch 5: Custom Action Test\n"; 
	$web_output = $web_output."<font face=\"Tahoma\" size=\"4\"><b>Test Batch Description: Test action logging to an advertiser with the \"Click Trumps Impression\" Flag set</b></font><br>"; 
	$commandline_output = $commandline_output."Test Batch Description: Test action logging to an advertiser with the \"Click Trumps Impression\" Flag set\n"; 
	$web_output = $web_output."<hr>"; 
	$commandline_output = $commandline_output."\n--------------------------------------------------------------------------------------------------------------------------\n"; 
	$web_output = $web_output."<font face=\"Tahoma\" size=\"3\" COLOR=\"#FF0000\"><b>Test 5.1: Send a click to the third custom action flight for the Canary Advertiser</b></font><br><br>"; 
	$commandline_output = $commandline_output."Test 5.1: Send a click to the third custom action flight for the Canary Advertiser\n\n"; 
    
	$url = "http://crtldev.aimatch.com:60000/canary/adclick/mid=".$testtime."5A/FCID=37/site=canary/area=custom_action_tracking/custom_action=3/trace=3";
	$output = MakeAdCall($url);
	$output_array = MakeOutputArray($output);
	$formatted_output = GetFormattedOutput($output_array,$web,$debug);
	$text1 = "A valid response was recieved to the click request ";
	$text2 = "";
	$text3 = "The 302 Redirect Location was: ";
	list($status51,$web_message1,$web_message2,$web_message3,$cl_message1,$cl_message2,$cl_message3) = CheckClickResponse($text1,$text2,$text3,$output_array,"tests.aimatch.com",$web,$debug);
	$web_output = $web_output.$web_message1.$web_message2.$web_message3."<hr>"; 
	$commandline_output = $commandline_output.$cl_message1.$cl_message2.$cl_message3."\n"; 

//
// 5.2 Request an impression from the third custom action flight for the Canary advertiser
// http://crtldev.aimatch.com:60000/canary/hserver/mid={random_number}/site=canary/area=custom_action_tracking/custom_action=3
//
	sleep(2); // sleep for two seconds before submitting the click
	$commandline_output = $commandline_output."          -----------------\n"; 
	$web_output = $web_output."<font face=\"Tahoma\" size=\"3\" COLOR=\"#FF0000\"><b>Test 5.2: Request an impression from the third custom action flight for the Canary advertiser</b><br></font><br>"; 
	$commandline_output = $commandline_output."Test 5.2: Request an impression from the third custom action flight for the Canary advertiser\n\n"; 
    $url = "http://crtldev.aimatch.com:60000/canary/hserver/mid=".$testtime."5A/site=canary/area=custom_action_tracking/custom_action=3/trace=3";
	$output = MakeAdCall($url);
	$output_array = MakeOutputArray($output);
	$formatted_output = GetFormattedOutput($output_array,$web,$debug);
	$text1 = "The correct ad was returned for the ad call that was made";
	$text2 = "";
	$text3 = "The response to the ad call returned:";
	list($status52,$web_message1,$web_message2,$web_message3,$cl_message1,$cl_message2,$cl_message3) = CheckResponse($text1,$text2,$text3,$output,"CustomActionTracking-3.jpg",$web);
	$web_output = $web_output.$web_message1.$web_message2.$web_message3."<hr>"; 
	$commandline_output = $commandline_output.$cl_message1.$cl_message2.$cl_message3."\n"; 
	
//
// 5.3 Send an action count request to the advertiser using just the ACTNAME= directive
// http://crtldev.aimatch.com:60000/canary/count/advid=1/actname=canarybirdseed
//
	$commandline_output = $commandline_output."          -----------------\n"; 
	$web_output = $web_output."<font face=\"Tahoma\" size=\"3\" COLOR=\"#FF0000\"><b>Test 5.3: Send an action count request to the advertiser using the ACTNAME= directive (this flight has the \"Clicks Trump Impressions\" flag set</b></font><br><br>"; 
	$commandline_output = $commandline_output."Test 5.3: Send an action count request to the advertiser using the ACTNAME= directive (this flight has the \"Clicks Trump Impressions\" flag set\n\n"; 
    $url = "http://crtldev.aimatch.com:60000/canary/count/mid=".$testtime."5A/advid=1/ACTNAME=canarybirdseed/trace=3";
	$output = MakeAdCall($url);
	$output_array = MakeOutputArray($output);
	$formatted_output = GetFormattedOutput($output_array,$web,$debug);
	$text1 = "A valid response was recieved from the Action Request ";
	$text2 = "";
	$text3 = "The response to the action request was:";
	list($status53,$web_message1,$web_message2,$web_message3,$cl_message1,$cl_message2,$cl_message3) = CheckResponse($text1,$text2,$text3,$output,"GIF89a",$web);
	$web_output = $web_output.$web_message1.$web_message2.$web_message3."<hr>"; 
	$commandline_output = $commandline_output.$cl_message1.$cl_message2.$cl_message3."\n"; 

//
// Custom Action Test #6: Test that no actions are logged to an advertisers without an action policy
// 6.1 Request an impression from the first custom action flight for the Sylvester advertiser
// http://crtldev.aimatch.com:60000/canary/hserver/mid={random_number}/site=sylvester/area=custom_action_tracking/custom_action=4
//
	$commandline_output = $commandline_output."\n--------------------------------------------------------------------------------------------------------------------------\n"; 
	$web_output = $web_output."<br><font face=\"Tahoma\" size=\"5\"><b>Test Batch 6: Custom Action Test</b></font><br>"; 
	$commandline_output = $commandline_output."Test Batch 6: Custom Action Test\n"; 
	$web_output = $web_output."<font face=\"Tahoma\" size=\"4\"><b>Test Batch Description: Test action logging to an advertiser using the ACTID directive</b></font><br>"; 
	$commandline_output = $commandline_output."Test Batch Description: Test action logging to an advertiser using the ACTID directive\n"; 
	$web_output = $web_output."<hr>"; 
	$commandline_output = $commandline_output."\n--------------------------------------------------------------------------------------------------------------------------\n"; 
	$web_output = $web_output."<font face=\"Tahoma\" size=\"3\" COLOR=\"#FF0000\"><b>Test 6.1: Test that no actions are logged to an advertisers without an action policy</b><br></font><br>"; 
	$commandline_output = $commandline_output."Test 6.1: Test that no actions are logged to an advertisers without an action policy\n\n"; 

    $url = "http://crtldev.aimatch.com:60000/canary/hserver/mid=".$testtime."6A/site=sylvester/area=custom_action_tracking/custom_action=4/trace=3";
	$output = MakeAdCall($url);
	$output_array = MakeOutputArray($output);
	$formatted_output = GetFormattedOutput($output_array,$web,$debug);
	$text1 = "The correct ad was returned for the ad call that was made";
	$text2 = "";
	$text3 = "The response to the ad call returned:";
	list($status61,$web_message1,$web_message2,$web_message3,$cl_message1,$cl_message2,$cl_message3) = CheckResponse($text1,$text2,$text3,$output,"CustomActionTracking-4.jpg",$web);
	$web_output = $web_output.$web_message1.$web_message2.$web_message3."<hr>"; 
	$commandline_output = $commandline_output.$cl_message1.$cl_message2.$cl_message3."\n"; 

//
// 6.2 Send a click to the first custom action flight for the Canary Advertiser
// http://crtldev.aimatch.com:60000/canary/adclick/FCID=30/site=sylvester/area=custom_action_tracking/custom_action=4
//
	sleep(2); // sleep for two seconds before submitting the click
	$commandline_output = $commandline_output."          -----------------\n"; 
	$web_output = $web_output."<font face=\"Tahoma\" size=\"3\" COLOR=\"#FF0000\"><b>Test 6.2: Send a click to the first custom action flight for the Canary advertiser</b></font><br><br>"; 
	$commandline_output = $commandline_output."Test 6.2: Send a click to the first custom action flight for the Canary advertiser\n\n"; 
    $url = "http://crtldev.aimatch.com:60000/canary/adclick/mid=".$testtime."6A/FCID=30/site=sylvester/area=custom_action_tracking/custom_action=4/trace=3";
	$output = MakeAdCall($url);
	$output_array = MakeOutputArray($output);
	$formatted_output = GetFormattedOutput($output_array,$web,$debug);
	$text1 = "A valid response was recieved to the click request ";
	$text2 = "";
	$text3 = "The 302 Redirect Location was: ";
	list($status62,$web_message1,$web_message2,$web_message3,$cl_message1,$cl_message2,$cl_message3) = CheckClickResponse($text1,$text2,$text3,$output_array,"tests.aimatch.com",$web,$debug);
	$web_output = $web_output.$web_message1.$web_message2.$web_message3."<hr>"; 
	$commandline_output = $commandline_output.$cl_message1.$cl_message2.$cl_message3."\n"; 

//
// 6.3 Send an action count request to the advertiser without an action policy
// http://crtldev.aimatch.com:60000/canary/count/advid=1/actid=7
//
	$commandline_output = $commandline_output."          -----------------\n"; 
	$web_output = $web_output."<font face=\"Tahoma\" size=\"3\" COLOR=\"#FF0000\"><b>Test 6.3: Send an action count request to the advertiser without an action policy</b></font><br><br>"; 
	$commandline_output = $commandline_output."Test 6.3: Send an action count request to the advertiser without an action policy\n\n"; 
    $url = "http://crtldev.aimatch.com:60000/canary/count/mid=".$testtime."6A/advid=2/ACTNAME=sylvestersufferinsuccotash/trace=3";
	$output = MakeAdCall($url);
	$output_array = MakeOutputArray($output);
	$formatted_output = GetFormattedOutput($output_array,$web,$debug);
	$text1 = "A valid response was recieved from the Action Request ";
	$text2 = "";
	$text3 = "The response to the action request was:";
	list($status63,$web_message1,$web_message2,$web_message3,$cl_message1,$cl_message2,$cl_message3) = CheckResponse($text1,$text2,$text3,$output,"GIF89a",$web);
	$web_output = $web_output.$web_message1.$web_message2.$web_message3."<hr>"; 
	$commandline_output = $commandline_output.$cl_message1.$cl_message2.$cl_message3."\n"; 

// pausing for four minutes for checkpoint to complete
sleep(240);


// Retrieve updated Numbers from the database
// Retrieve the query results
$after_result=mysql_query($query);

// Determine the number of rows in the result set.
if($after_result){
    $after_num=mysql_numrows($after_result); }
else {
    $after_num=0; }

$i=0;
$web_output = $web_output."<br><font face=\"Tahoma\" size=\"3\"><b>Table Results After Test:</b></font><br>"; 
$commandline_output = $commandline_output."Table Results After Test:\n"; 

while($i < $after_num){
    $after_FlightCreativeID[$i]=mysql_result($after_result,$i,"s2.flight_creative_id");
    $after_Site[$i]=mysql_result($after_result,$i,"s2.site");
    $after_Area[$i]=mysql_result($after_result,$i,"s2.area");
    $after_Served[$i]=mysql_result($after_result,$i,"s2.served");
    $after_Clicked[$i]=mysql_result($after_result,$i,"s2.clicked");
    $after_BSActed[$i]=mysql_result($after_result,$i,"s2.acted");
    $after_ASActed[$i]=mysql_result($after_result,$i,"s1.acted");
    $after_ActionID[$i]=mysql_result($after_result,$i,"s1.action_id");
    $after_Attribution[$i]=mysql_result($after_result,$i,"s1.attribution");
	$web_output = $web_output."<font face=\"Tahoma\" size=\"3\">FlightID: ".$after_FlightCreativeID[$i]." | "; 
	$commandline_output = $commandline_output."     FlightID: ".$after_FlightCreativeID[$i]." | "; 
	$web_output = $web_output."Site: ".$after_Site[$i]." | "; 
	$commandline_output = $commandline_output."Site: ".$after_Site[$i]." | "; 
	$web_output = $web_output."Area: ".$after_Area[$i]." | "; 
	$commandline_output = $commandline_output." Area: ".$after_Area[$i]." | "; 
	$web_output = $web_output."Served: ".$after_Served[$i]." | "; 
	$commandline_output = $commandline_output."Served: ".$after_Served[$i]." | "; 
	$web_output = $web_output."Clicked: ".$after_Clicked[$i]." | "; 
	$commandline_output = $commandline_output." Clicked: ".$after_Clicked[$i]." | "; 
	$web_output = $web_output."BSActed: ".$after_BSActed[$i]." | "; 
	$commandline_output = $commandline_output." BSActed: ".$after_BSActed[$i]." | "; 
	$web_output = $web_output."ASActed: ".$after_ASActed[$i]." | "; 
	$commandline_output = $commandline_output." ASActed: ".$after_ASActed[$i]." | "; 
	$web_output = $web_output."ActionID: ".$after_ActionID[$i]." | "; 
	$commandline_output = $commandline_output." ActionID: ".$after_ActionID[$i]." | "; 
	$web_output = $web_output."Attribution: ".$after_Attribution[$i]."</font><br>"; 
	$commandline_output = $commandline_output." Attribution: ".$after_Attribution[$i]."\n"; 

    $check_Site[$i]=$after_Site[$i]-$before_Site[$i];
	if($check_Site[$i] == 0) {
		}
	else {
		}
    $check_FlightCreativeID[$i]=trim($after_FlightCreativeID[$i])-trim($before_FlightCreativeID[$i]);
    $check_Area[$i]=trim($after_Area[$i])-trim($before_Area[$i]);
    $check_Served[$i]=trim($after_Served[$i])-trim($before_Served[$i]);
    $check_Clicked[$i]=trim($after_Clicked[$i])-trim($before_Clicked[$i]);
    $check_BSActed[$i]=trim($after_BSActed[$i])-trim($before_BSActed[$i]);
    $check_ASActed[$i]=trim($after_ASActed[$i])-trim($before_ASActed[$i]);
    $check_ActionID[$i]=trim($after_ActionID[$i])-trim($before_ActionID[$i]);
    $check_Attribution[$i]=trim($after_Attribution[$i])-trim($before_Attribution[$i]);
    $i++;
}


// Figure out the differences
$web_output = $web_output."<br><font face=\"Tahoma\" size=\"4\"><b>Checking Results</b></font><br>"; 
$commandline_output = $commandline_output."\nChecking Results\n"; 
if($after_num == $before_num){
	$web_output = $web_output."<font face=\"Tahoma\" size=\"3\">Number of items returned is okay: ".$before_num."</font><br>"; 
	$commandline_output = $commandline_output."Number of items returned is okay: ".$before_num."\n\n"; }
else {
	$web_output = $web_output."<font face=\"Tahoma\" size=\"3\" COLOR=\"#FF0000\">Number of items returned are different after the test (".$after_num.") - the number of items should be the same as before the test (".$before_num.").</font><br>"; 
	$commandline_output = $commandline_output."Number of items returned are different after the test (".$after_num.") - the number of items should be the same as before the test (".$before_num.").\n\n"; }

$i=0;

$web_output = $web_output."<br><font face=\"Tahoma\" size=\"3\"><b>Table Differences:</b></font><br>"; 
$commandline_output = $commandline_output."Table Differences:\n"; 

while($i < $after_num){
	$web_output = $web_output."<font face=\"Tahoma\" size=\"3\">FlightID: ".$after_FlightCreativeID[$i]." | "; 
	$commandline_output = $commandline_output."     FlightID: ".$after_FlightCreativeID[$i]." | "; 
	$web_output = $web_output."Site: ".$check_Site[$i]." | "; 
	$commandline_output = $commandline_output." Site: ".$check_Site[$i]." | "; 
	$web_output = $web_output."Area: ".$check_Area[$i]." | "; 
	$commandline_output = $commandline_output." Area: ".$check_Area[$i]." | "; 
	$web_output = $web_output."Served: ".$check_Served[$i]." | "; 
	$commandline_output = $commandline_output." Served: ".$check_Served[$i]." | "; 
	$web_output = $web_output."Clicked: ".$check_Clicked[$i]." | "; 
	$commandline_output = $commandline_output." Clicked: ".$check_Clicked[$i]." | "; 
	$web_output = $web_output."BSActed: ".$check_BSActed[$i]." | "; 
	$commandline_output = $commandline_output." BSActed: ".$check_BSActed[$i]." | "; 
	$web_output = $web_output."ASActed: ".$check_ASActed[$i]." | "; 
	$commandline_output = $commandline_output." ASActed: ".$check_ASActed[$i]." | "; 
	$web_output = $web_output."ActionID: ".$check_ActionID[$i]." | "; 
	$commandline_output = $commandline_output." ActionID: ".$check_ActionID[$i]." | "; 
	$web_output = $web_output."Attribution: ".$check_Attribution[$i]."</font><br>"; 
	$commandline_output = $commandline_output." Attribution: ".$check_Attribution[$i]."\n"; 
    $i++;
}

// Figure out if the tests were successful.
// Are the responses all valid?
if ($status11 == "pass" && $status12 == "pass" && $status13 == "pass" && $status14 == "pass") {
	$web_response_status = $web_response_status."<font face=\"Tahoma\" size=\"3\" COLOR=\"GREEN\">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;SUCCESS: All ACTION responses from Test Batch 1 are valid</font><br>"; 
	$commandline_response_status = $commandline_response_status."     SUCCESS: All ACTION responses from Test Batch 1 are valid\n"; 
	$batch1_status = "pass";
}
else {
	$web_response_status = $web_response_status."<font face=\"Tahoma\" size=\"3\" COLOR=\"RED\">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;FAILURE: Some ACTION responses from Test Batch 1 are not valid</font><br>"; 
	$commandline_response_status = $commandline_response_status."     FAILURE: Some ACTION responses from Test Batch 1 are not valid\n"; 
	$batch1_status = "fail";
}

if ($status21 == "pass" && $status22 == "pass" && $status23 == "pass" && $status24 == "pass") {
	$web_response_status = $web_response_status."<font face=\"Tahoma\" size=\"3\" COLOR=\"GREEN\">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;SUCCESS: All ACTION responses from Test Batch 2 are valid</font><br>"; 
	$commandline_response_status = $commandline_response_status."     SUCCESS: All ACTION responses from Test Batch 2 are valid\n"; 
	$batch2_status = "pass";
}
else {
	$web_response_status = $web_response_status."<font face=\"Tahoma\" size=\"3\" COLOR=\"RED\">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;FAILURE: Some ACTION responses from Test Batch 2 are not valid</font><br>"; 
	$commandline_response_status = $commandline_response_status."     FAILURE: Some ACTION responses from Test Batch 2 are not valid\n"; 
	$batch2_status = "fail";
}

if ($status31 == "pass" && $status32 == "pass" && $status33 == "pass") {
	$web_response_status = $web_response_status."<font face=\"Tahoma\" size=\"3\" COLOR=\"GREEN\">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;SUCCESS: All ACTION responses from Test Batch 3 are valid</font><br>"; 
	$commandline_response_status = $commandline_response_status."     SUCCESS: All ACTION responses from Test Batch 3 are valid\n"; 
	$batch3_status = "pass";
}
else {
	$web_response_status = $web_response_status."<font face=\"Tahoma\" size=\"3\" COLOR=\"RED\">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;FAILURE: Some ACTION responses from Test Batch 3 are not valid</font><br>"; 
	$commandline_response_status = $commandline_response_status."     FAILURE: Some ACTION responses from Test Batch 3 are not valid\n"; 
	$batch3_status = "fail";
}

if ($status41 == "pass" && $status42 == "pass" && $status43 == "pass") {
	$web_response_status = $web_response_status."<font face=\"Tahoma\" size=\"3\" COLOR=\"GREEN\">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;SUCCESS: All ACTION responses from Test Batch 4 are valid</font><br>"; 
	$commandline_response_status = $commandline_response_status."     SUCCESS: All ACTION responses from Test Batch 4 are valid\n"; 
	$batch4_status = "pass";
}
else {
	$web_response_status = $web_response_status."<font face=\"Tahoma\" size=\"3\" COLOR=\"RED\">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;FAILURE: Some ACTION responses from Test Batch 4 are not valid</font><br>"; 
	$commandline_response_status = $commandline_response_status."     FAILURE: Some ACTION responses from Test Batch 4 are not valid\n"; 
	$batch4_status = "fail";
}

if ($status51 == "pass" && $status52 == "pass" && $status53 == "pass") {
	$web_response_status = $web_response_status."<font face=\"Tahoma\" size=\"3\" COLOR=\"GREEN\">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;SUCCESS: All ACTION responses from Test Batch 5 are valid</font><br>"; 
	$commandline_response_status = $commandline_response_status."     SUCCESS: All ACTION responses from Test Batch 5 are valid\n"; 
	$batch5_status = "pass";
}
else {
	$web_response_status = $web_response_status."<font face=\"Tahoma\" size=\"3\" COLOR=\"RED\">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;FAILURE: Some ACTION responses from Test Batch 5 are not valid</font><br>"; 
	$commandline_response_status = $commandline_response_status."     FAILURE: Some ACTION responses from Test Batch 5 are not valid\n"; 
	$batch5_status = "fail";
}

if ($status61 == "pass" && $status62 == "pass" && $status63 == "pass") {
	$web_response_status = $web_response_status."<font face=\"Tahoma\" size=\"3\" COLOR=\"GREEN\">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;SUCCESS: All ACTION responses from Test Batch 6 are valid</font><br>"; 
	$commandline_response_status = $commandline_response_status."     SUCCESS: All ACTION responses from Test Batch 6 are valid\n"; 
	$batch6_status = "pass";
}
else {
	$web_response_status = $web_response_status."<font face=\"Tahoma\" size=\"3\" COLOR=\"RED\">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;FAILURE: Some ACTION responses from Test Batch 6 are not valid</font><br>"; 
	$commandline_response_status = $commandline_response_status."     FAILURE: Some ACTION responses from Test Batch 6 are not valid\n"; 
	$batch6_status = "fail";
}

// 
if($batch1_status = "pass" && $batch2_status = "pass" && $batch3_status = "pass" && $batch4_status = "pass" && $batch5_status = "pass" && $batch6_status = "pass"){
	$web_overall_response_status = $web_overall_response_status."<font face=\"Tahoma\" size=\"3\" COLOR=\"GREEN\"><b>SUCCESS: All Action Responses are valid</b></font><br>"; 
	$commandline_overall_response_status = $commandline_overall_response_status."\nSUCCESS: All Action Responses are valid\n"; 
	$response_status = "pass";
}
else {
	$web_overall_response_status = $web_overall_response_status."<font face=\"Tahoma\" size=\"3\" COLOR=\"GREEN\"><b>FAILURE: Some of the ACTION responses are not valid</b></font><br>"; 
	$commandline_overall_response_status = $commandline_overall_response_status."\nFAILURE: Some of the ACTION responses are not valid\n"; 
	$response_status = "fail";
}

// Are the items in the database valid?

$i=0;

while($i < $after_num){
	if($after_FlightCreativeID[$i]==30) {
		if($check_FlightCreativeID[$i] == 0 && $check_Site[$i] == 0 && $check_Area[$i] == 0 && $check_Served[$i] == 1 && $check_Clicked[$i] == 1 &&
		$check_BSActed[$i] == 0 && $check_ASActed[$i] == 0 && $check_ActionID[$i] == 0 && $check_Attribution[$i] == 0) {
			$web_action_status = $web_action_status."<font face=\"Tahoma\" size=\"3\" COLOR=\"GREEN\">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;SUCCESS: All items were logged correctly for Flight Creative ID=".$after_FlightCreativeID[$i]."</font><br>"; 
			$commandline_action_status = $commandline_action_status."     SUCCESS: All items were logged correctly for Flight Creative ID=".$after_FlightCreativeID[$i]."\n"; 
			$a_status30 = "pass";
		}
		else {
			$web_action_status = $web_action_status."<font face=\"Tahoma\" size=\"3\" COLOR=\"RED\">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;FAILURE: All items were logged correctly for Flight Creative ID=".$after_FlightCreativeID[$i]."</font><br>"; 
			$commandline_action_status = $commandline_action_status."     FAILURE: All items were NOT logged correctly for Flight Creative ID=".$after_FlightCreativeID[$i]."\n"; 
			$a_status30 = "fail";
		}}

	if($after_FlightCreativeID[$i]==31) {
		if($check_FlightCreativeID[$i] == 0 && $check_Site[$i] == 0 && $check_Area[$i] == 0 && $check_Served[$i] == 1 && $check_Clicked[$i] == 0 &&
		$check_BSActed[$i] == 0 && $check_ASActed[$i] == 0 && $check_ActionID[$i] == 0 && $check_Attribution[$i] == 0) {
			$web_action_status = $web_action_status."<font face=\"Tahoma\" size=\"3\" COLOR=\"GREEN\">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;SUCCESS: All items were logged correctly for Flight Creative ID=".$after_FlightCreativeID[$i]."</font><br>"; 
			$commandline_action_status = $commandline_action_status."     SUCCESS: All items were logged correctly for Flight Creative ID=".$after_FlightCreativeID[$i]."\n"; 
			$a_status31 = "pass";
		}
		else {
			$web_action_status = $web_action_status."<font face=\"Tahoma\" size=\"3\" COLOR=\"RED\">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;FAILURE: All items were logged correctly for Flight Creative ID=".$after_FlightCreativeID[$i]."</font><br>"; 
			$commandline_action_status = $commandline_action_status."     FAILURE: All items were NOT logged correctly for Flight Creative ID=".$after_FlightCreativeID[$i]."\n"; 
			$a_status31 = "fail";
		}}
		
	if($after_FlightCreativeID[$i]==32) {
		if($check_FlightCreativeID[$i] == 0 && $check_Site[$i] == 0 && $check_Area[$i] == 0 && $check_Served[$i] == 0 && $check_Clicked[$i] == 1 &&
		$check_BSActed[$i] == 0 && $check_ASActed[$i] == 0 && $check_ActionID[$i] == 0 && $check_Attribution[$i] == 0) {
			$web_action_status = $web_action_status."<font face=\"Tahoma\" size=\"3\" COLOR=\"GREEN\">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;SUCCESS: All items were logged correctly for Flight Creative ID=".$after_FlightCreativeID[$i]."</font><br>"; 
			$commandline_action_status = $commandline_action_status."     SUCCESS: All items were logged correctly for Flight Creative ID=".$after_FlightCreativeID[$i]."\n"; 
			$a_status32 = "pass";
		}
		else {
			$web_action_status = $web_action_status."<font face=\"Tahoma\" size=\"3\" COLOR=\"RED\">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;FAILURE: All items were logged correctly for Flight Creative ID=".$after_FlightCreativeID[$i]."</font><br>"; 
			$commandline_action_status = $commandline_action_status."     FAILURE: All items were NOT logged correctly for Flight Creative ID=".$after_FlightCreativeID[$i]."\n"; 
			$a_status32 = "fail";
		}}
		
	if($after_FlightCreativeID[$i]==34) {
		if($check_FlightCreativeID[$i] == 0 && $check_Site[$i] == 0 && $check_Area[$i] == 0 && $check_Served[$i] == 1 && $check_Clicked[$i] == 0 &&
		$check_BSActed[$i] == 1 && $check_ASActed[$i] == 0 && $check_ActionID[$i] == 0 && $check_Attribution[$i] == 0) {
			$web_action_status = $web_action_status."<font face=\"Tahoma\" size=\"3\" COLOR=\"GREEN\">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;SUCCESS: All items were logged correctly for Flight Creative ID=".$after_FlightCreativeID[$i]."</font><br>"; 
			$commandline_action_status = $commandline_action_status."     SUCCESS: All items were logged correctly for Flight Creative ID=".$after_FlightCreativeID[$i]."\n"; 
			$a_status34 = "pass";
		}
		else {
			$web_action_status = $web_action_status."<font face=\"Tahoma\" size=\"3\" COLOR=\"RED\">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;FAILURE: All items were logged correctly for Flight Creative ID=".$after_FlightCreativeID[$i]."</font><br>"; 
			$commandline_status = $commandline_status."     FAILURE: All items were NOT logged correctly for Flight Creative ID=".$after_FlightCreativeID[$i]."\n"; 
			$a_status34 = "fail";
		}}
		
	if($after_FlightCreativeID[$i]==35) {
		if($check_FlightCreativeID[$i] == 0 && $check_Site[$i] == 0 && $check_Area[$i] == 0 && $check_Served[$i] == 1 && $check_Clicked[$i] == 1 &&
		$check_BSActed[$i] == 0 && $check_ASActed[$i] == 1 && $check_ActionID[$i] == 0 && $check_Attribution[$i] == 0) {
			$web_action_status = $web_action_status."<font face=\"Tahoma\" size=\"3\" COLOR=\"GREEN\">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;SUCCESS: All items were logged correctly for Flight Creative ID=".$after_FlightCreativeID[$i]."</font><br>"; 
			$commandline_action_status = $commandline_action_status."     SUCCESS: All items were logged correctly for Flight Creative ID=".$after_FlightCreativeID[$i]."\n"; 
			$a_status35 = "pass";
		}
		else {
			$web_action_status = $web_action_status."<font face=\"Tahoma\" size=\"3\" COLOR=\"RED\">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;FAILURE: All items were logged correctly for Flight Creative ID=".$after_FlightCreativeID[$i]."</font><br>"; 
			$commandline_action_status = $commandline_action_status."     FAILURE: All items were NOT logged correctly for Flight Creative ID=".$after_FlightCreativeID[$i]."\n"; 
			$a_status35 = "fail";
		}}

		if($after_FlightCreativeID[$i]==36) {
		if($check_FlightCreativeID[$i] == 0 && $check_Site[$i] == 0 && $check_Area[$i] == 0 && $check_Served[$i] == 1 && $check_Clicked[$i] == 1 &&
		$check_BSActed[$i] == 0 && $check_ASActed[$i] == 1 && $check_ActionID[$i] == 0 && $check_Attribution[$i] == 0) {
			$web_action_status = $web_action_status."<font face=\"Tahoma\" size=\"3\" COLOR=\"GREEN\">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;SUCCESS: All items were logged correctly for Flight Creative ID=".$after_FlightCreativeID[$i]."</font><br>"; 
			$commandline_action_status = $commandline_action_status."     SUCCESS: All items were logged correctly for Flight Creative ID=".$after_FlightCreativeID[$i]."\n"; 
			$a_status36 = "pass";
		}
		else {
			$web_action_status = $web_action_status."<font face=\"Tahoma\" size=\"3\" COLOR=\"RED\">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;FAILURE: All items were logged correctly for Flight Creative ID=".$after_FlightCreativeID[$i]."</font><br>"; 
			$commandline_action_status = $commandline_action_status."     FAILURE: All items were NOT logged correctly for Flight Creative ID=".$after_FlightCreativeID[$i]."\n"; 
			$a_status36 = "fail";
		}}
		
	if($after_FlightCreativeID[$i]==37) {
		if($check_FlightCreativeID[$i] == 0 && $check_Site[$i] == 0 && $check_Area[$i] == 0 && $check_Served[$i] == 1 && $check_Clicked[$i] == 1 &&
		$check_BSActed[$i] == 0 && $check_ASActed[$i] == 1 && $check_ActionID[$i] == 0 && $check_Attribution[$i] == 0) {
			$web_action_status = $web_action_status."<font face=\"Tahoma\" size=\"3\" COLOR=\"GREEN\">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;SUCCESS: All items were logged correctly for Flight Creative ID=".$after_FlightCreativeID[$i]."</font><br>"; 
			$commandline_action_status = $commandline_action_status."     SUCCESS: All items were logged correctly for Flight Creative ID=".$after_FlightCreativeID[$i]."\n"; 
			$a_status37 = "pass";
		}
		else {
			$web_action_status = $web_action_status."<font face=\"Tahoma\" size=\"3\" COLOR=\"RED\">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;FAILURE: All items were logged correctly for Flight Creative ID=".$after_FlightCreativeID[$i]."</font><br>"; 
			$commandline_action_status = $commandline_action_status."     FAILURE: All items were NOT logged correctly for Flight Creative ID=".$after_FlightCreativeID[$i]."\n"; 
			$a_status37 = "fail";
		}}

	if($after_FlightCreativeID[$i]==38) {
		if($check_FlightCreativeID[$i] == 0 && $check_Site[$i] == 0 && $check_Area[$i] == 0 && $check_Served[$i] == 0 && $check_Clicked[$i] == 1 &&
		$check_BSActed[$i] == 1 && $check_ASActed[$i] == 0 && $check_ActionID[$i] == 0 && $check_Attribution[$i] == 0) {
			$web_action_status = $web_action_status."<font face=\"Tahoma\" size=\"3\" COLOR=\"GREEN\">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;SUCCESS: All items were logged correctly for Flight Creative ID=".$after_FlightCreativeID[$i]."</font><br>"; 
			$commandline_action_status = $commandline_action_status."     SUCCESS: All items were logged correctly for Flight Creative ID=".$after_FlightCreativeID[$i]."\n"; 
			$a_status38 = "pass";
		}
		else {
			$web_action_status = $web_action_status."<font face=\"Tahoma\" size=\"3\" COLOR=\"RED\">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;FAILURE: All items were logged correctly for Flight Creative ID=".$after_FlightCreativeID[$i]."</font><br>"; 
			$commandline_action_status = $commandline_action_status."     FAILURE: All items were NOT logged correctly for Flight Creative ID=".$after_FlightCreativeID[$i]."\n"; 
			$a_status38 = "fail";
		}}
		
	$i++;
}

if($a_status30 == "pass" && $a_status31 == "pass" && $a_status32 == "pass" && $a_status34 == "pass" && 
$a_status35 == "pass" && $a_status36 == "pass" && $a_status37 == "pass" && $a_status38 == "pass") { 
	$web_overall_action_status = $web_overall_action_status."<br><font face=\"Tahoma\" size=\"3\" COLOR=\"GREEN\"><b>SUCCESS: All ACTION Items logged in the database are valid</b></font><br>"; 
	$commandline_overall_action_status = $commandline_overall_action_status."\nSUCCESS: All ACTION Items logged in the database are valid\n"; 
	$action_status = "pass";
}
else {
	$web_overall_action_status = $web_overall_action_status."<br><font face=\"Tahoma\" size=\"3\" COLOR=\"RED\"><b>FAILURE: All ACTION Items logged in the database are NOT valid</b></font><br>"; 
	$commandline_overall_action_status = $commandline_overall_action_status."\nFAILURE: All ACTION Items logged in the database are NOT valid\n"; 
	$action_status = "fail";
}

if($response_status == "pass" && $action_status == "pass") { 
	$web_test_status = $web_test_status."<font face=\"Tahoma\" size=\"3\" COLOR=\"GREEN\"><b>SUCCESS: Canary Action Test was successful</b></font><br><br>"; 
	$commandline_test_status = $commandline_test_status."SUCCESS: Canary Action Test was successful\n\n"; 
	$action_status = "pass";
}
else {
	$web_test_status = $web_test_status."<font face=\"Tahoma\" size=\"3\" COLOR=\"RED\"><b>FAILURE: Canary Action Test was not successful</b></font><br><br>"; 
	$commandline_test_status = $commandline_test_status."FAILURE: Canary Action Test was not successful\n\n"; 
	$action_status = "fail";
}

$to_file = $commandline_test_status.$commandline_overall_response_status.$commandline_response_status.$commandline_overall_action_status.$commandline_action_status.$commandline_output;

if (isset($web)) { 
	echo $web_test_status.$web_overall_response_status.$web_response_status.$web_overall_action_status.$web_action_status.$web_output; }
else {
	echo $to_file; }


$filestatus = WriteResultsFile($status,$testshortname,$testname,$testdesc,$testurl,$testtime,$web,$action_status,$to_file);

if (isset($debug)) { phpinfo(); }

//================= Functions =================
	
function WriteResultsFile($status,$testshortname,$testname,$testdesc,$testurl,$testtime,$web,$status,$to_file){
//	$debug = 1;
	// Setup the archival file name for the test
	$datestamp = date("YmdGis");
	$directory = "/http/matchtest/archived_results/actiontrackingtest/";
	if(!is_dir($directory)){
		if(!mkdir($directory, 0777)) {
			die('Failed to create directory: '.$directory);
		}
	}

	// Write out the information to the testresults.txt file.
	$lines = file('/http/matchtest/testresults.txt');
	$numitems = count($lines);

	// Loop through our array, show HTML source as HTML source; and line numbers too.

	if (isset($debug)) { echo "<br><pre>HTTP Response Output:<br><br>"; print_r($lines); echo "</pre><br>"; }

	$newfile = $lines[0];
	$i=1;
	while($i <= $numitems) {
		$pieces = explode("|",$lines[$i]);
		if($testname == $pieces[1]){
			$replaced_line = $status."|".$testname."|".$testdesc."|".$testurl."|".$testtime."\n";
			$newfile = $newfile.$replaced_line;
		}
		else{
			$newfile = $newfile.$lines[$i];
		}
		$i++;
	}

	if (isset($debug)) { echo "<br><pre>New File Output:<br><br>"; print_r($newfile); echo "</pre><br>"; }

	$fp = fopen('/http/matchtest/testresults.txt', 'w');

	fwrite($fp, $newfile);
	fclose($fp);

// Write out results of the test to a file 
	if (isset($debug)) { echo "Filename: ".$directory.$testshortname.".web.".$status.".".$datestamp.".".$testtime.".txt<br>"; }

	if (isset($web)) { $testfilename = $directory.$testshortname.".web.".$status.".".$datestamp.".".$testtime.".txt"; }
	else { $testfilename = $directory.$testshortname.".commandline.".$status.".".$datestamp.".".$testtime.".txt"; }

	if (!$fp1 = fopen($testfilename,'w')) {
		echo "Cannot open file ($testfilename)";
		exit;
	}

	fwrite ($fp1,$to_file."\n\n");
	fclose($fp1);

	return 0;
}

function MakeAdCall($url) {
	$UserAgent="Mozilla/5.0 (Windows; U; Windows NT 6.1; en-US; rv:1.9.2.16) Gecko/20110319 Firefox/3.6.16";

    // initilize the curl call
    $ch = curl_init();
    // set useragent
    curl_setopt($ch, CURLOPT_USERAGENT, $UserAgent);
    // Set curl to use $url for http call
    curl_setopt($ch, CURLOPT_URL, $url);
    // show client headers
    curl_setopt($ch, CURLOPT_HEADER, 1);
    // have curl put X-Forwarded-For into headers
    curl_setopt($ch,CURLOPT_HTTPHEADER, $client_headers);
    // store results as the return of curl_exec
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE);
    // Follow redirects
    curl_setopt($ch, CURLOPT_FOLLOWLOCATION, TRUE);		
    // if HTML error 400 over over, fail
    curl_setopt($ch, CURLOPT_FAILONERROR, TRUE);

    $output = curl_exec($ch);
    if (isset($debug)) { echo "<br><pre>HTTP Response Output:<br><br>"; print_r($output); echo "</pre><br>"; }
    //close curl connection
    curl_close($ch);

return $output;
}


function MakeOutputArray($output){
        $output_array = preg_split('/[\n]/', $output, -1, PREG_SPLIT_DELIM_CAPTURE | PREG_SPLIT_NO_EMPTY);
        if (isset($debug)) { echo "<br><pre>HTTP Response Output (array):<br><br>"; print_r($output_array); echo "</pre><br>"; }

return $output_array;
}


function TraceInfo($output,$string,$debug) {

        $line_match = array_values(preg_grep("/$string/", $output));
            if (isset($GLOBALS["debug"])) { echo "DEBUG - string:  ".$string."<br>";}
            if (isset($GLOBALS["debug"])) { echo "DEBUG - output:  <br><pre>"; print_r($output); echo "</pre><br>";}
            if (isset($GLOBALS["debug"])) { echo "DEBUG - line_match:<br><pre>"; print_r($line_match); echo "</pre><br>";}
        if(stripos($line_match[0], " = ")){
			$explode = explode(" = ", $line_match[0]); }
        if(stripos($line_match[0], ":")){
			$explode = explode(":", $line_match[0]); }
        if(stripos($line_match[1], ">>>>")){
			$result = $line_match[1]; }
        if (isset($GLOBALS["debug"])) { echo "DEBUG - result:  ".$result."<br>";}
		return $result;
}

function GetFormattedOutput($output_array, $web,$debug){
        $http_response = array_values(preg_grep("/^HTTP/", $output_array));
        $http_array = explode(" ",$http_response[0]);
        $http_return_code = trim($http_array[1]);
        $http_return_text = trim($http_array[2]);
//        if (isset($debug)) { echo "<br>DEBUG - HTTP CODE/TEXT: $http_return_code / $http_return_text<br>"; }

        /* check code = 200 or 302 and text equal OK, anything else we have an error */

        if (($http_return_text != "OK" || $http_return_code != "200")&&($http_return_text != "Relocate" || $http_return_code != "302")) {
//            if (isset($debug)) { echo "***** ALERT - HTTP CODE/TEXT: ".trim($http_response[0])." *****<br>"; }
				echo "HTTP return code/text was not 200/OK". trim($http_response[0])."<br>";
                die;
        }
		
		// count the number of elements in the response array
		$num_elements = count($output_array);
//        if (isset($debug)) { echo "The Number Of Elements in the OUTPUT Array are: ".$num_elements." <br>"; }
		
		// format the items in the array as a long concatenated string.
		$y=0;
		while($y <= $num_elements){
			$formatted_output = $formatted_output.$output_array[$y]."\n";
			$formatted_output_web = $formatted_output_web.$output_array[$y]."<br>";
			$y++;
		}
//        if (isset($debug)) { echo "The Formatted contents of OUTPUT are: ".$formatted_output_web." *****<br>"; }
		
		if($web) {
			return $formatted_output_web; }
		else {
			return $formatted_output; }
}

function CheckResponse($text1,$text2,$text3,$output,$SearchString,$web){

		$response = print_r($output,TRUE);
		if(stripos($output, $SearchString)){
			$message1 = "***** SUCCESS - ".$text1;
			$message2 = $text2;
			$message3 = $text3;
			$web_message1 = $message1."<br>";
			$cl_message1 = $message1."\n"; 
            $web_message2 = $message2."";
			$cl_message2 = $message2."\n"; 
			$web_message3 = "<pre>".$message3."<br><br><textarea readonly name=\"text\" style=\"width: 1300px; height: 300px\">".$response."</textarea></pre><br>"; 
			$cl_message3 = "".$message3."\n".$response."\n"; 
			$status = "pass";
        }
        else {
			$message1 = "***** ALERT - Targeting doesn't match. Targeting IS NOT WORKING: $TraceTargeting";
			$message2 = "***** Writing out failure to testresults.txt";
			$message3 = "HTTP Response Output:";
            $web_message1 = $message1."<br>"; 
            $cl_message1 = $message1."\n"; 
            $web_message2 = $message2."";
            $cl_message2 = $message2."\n";
			$web_message3 = "<br><pre>".$message3."<br><br>".$response."</pre><br>"; 
			$cl_message3 = "\n".$message3."\n".$response."\n"; 
			$status = "fail";			
		}
	return array($status,$web_message1,$web_message2,$web_message3,$cl_message1,$cl_message2,$cl_message3);
}

function CheckClickResponse($text1,$text2,$text3,$output_array,$SearchString,$web,$debug){

        $http_response1 = array_values(preg_grep("/^HTTP/", $output_array));
        $http_array = explode(" ",$http_response1[0]);
        $http_return_code = trim($http_array[1]);
        $http_return_text = trim($http_array[2]);

        $http_response2 = array_values(preg_grep("/^Location:/", $output_array));
        $http_array2 = explode(" ",$http_response2[0]);
        $http_location_destination = trim($http_array2[1]);
	
        if (isset($debug)) { echo "The Elements of the HTTP Respons Array are: <br>
		http_return_code: ".$http_return_code."<br>
		http_return_text: ".$http_return_text."<br>
		http_location_destination: ".$http_location_destination."<br>
		*****<br>"; }

        if (isset($debug)) { echo "SearchString is: ".$SearchString."<br>"; }
		
		
        if(stripos($http_location_destination, $SearchString)){
			$message1 = "***** SUCCESS - ".$text1;
			$message2 = $text2;
			$message3 = $text3;
			if (isset($web)) { $web_message1 = $message1."<br>";}
			else { $cl_message1 = $message1."\n"; }
            if (isset($web)) { $web_message2 = $message2."";}
			else { $cl_message2 = $message2."\n"; }
			if (isset($web)) { $web_message3 = $message3." ".$http_location_destination."<br><br>"; }
			else { $cl_message3 = $message3." ".$http_location_destination."\n"; }
			$status = "pass";
        }
        else {
			$message1 = "***** ALERT - Targeting doesn't match. Targeting IS NOT WORKING: $TraceTargeting";
			$message2 = "***** Writing out failure to testresults.txt";
			$message3 = "HTTP Response Output:";
            if (isset($web)) { $web_message1 = $message1."<br>"; }
            else { $cl_message1 = $message1."\n"; }
            if (isset($web)) { $web_message2 = $message2."";}
            else { $cl_message2 = $message2."\n";}
			if (isset($web)) { $web_message3 = $message3." ".$http_location_destination."<br><br>"; }
			else { $cl_message3 = $message3." ".$http_location_destination."\n"; }
			$status = "fail";			
		}
	return array($status,$web_message1,$web_message2,$web_message3,$cl_message1,$cl_message2,$cl_message3);
}

?>
